package utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class CommonCheck {
	/**
	 * 检测是否为正确格式的用户名（6到20位<b>字母 数字 . _</b>）
	 * 
	 * @param uname
	 * @return
	 */
	public static boolean checkUname(String uname) {
		if (uname == null) {
			return false;
		}
		if (Pattern.matches("^[.A-Za-z0-9_]{6,20}$", uname)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确格式的密码（6到15位<b>字母 数字 . _ -</b>）
	 * 
	 * @param password
	 * @return
	 */
	public static boolean checkPassword(String password) {
		if (password == null) {
			return false;
		}
		if (Pattern.matches("^[.A-Za-z0-9_-]{6,15}$", password)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确格式的日期
	 * 
	 * @param date
	 * @return
	 */
	public static boolean checkDateTime(String date) {
		if (date == null) {
			return false;
		}
		if (Pattern.matches("^\\d{4}.\\d{1,2}.\\d{1,2}$", date)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否全部为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkDigital(String str) {
		if (str == null) {
			return false;
		}
		if (Pattern.matches("\\d+", str)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为一个数
	 * 
	 * @param num
	 * @return
	 */
	public static boolean checkNum(String num) {
		if (num == null) {
			return false;
		}
		if (Pattern.matches("\\d+\\.?\\d*", num)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确的身份证
	 * 
	 * @param cardid
	 * @return
	 */
	public static boolean checkCardId(String cardid) {
		if (cardid == null || "".equals(cardid)) {
			return false;
		}
		@SuppressWarnings("unused")
		String[] errors = new String[] { "验证通过!", "身份证号码位数不对!",
				"身份证号码出生日期超出范围或含有非法字符!", "身份证号码校验错误!", "身份证地区非法!" };
		Map<String, String> areas = new HashMap<String, String>();
		areas.put("11", "北京");
		areas.put("12", "天津");
		areas.put("13", "河北");
		areas.put("14", "山西");
		areas.put("15", "内蒙古");
		areas.put("21", "辽宁");
		areas.put("22", "吉林");
		areas.put("23", "黑龙江");
		areas.put("31", "上海");
		areas.put("32", "江苏");
		areas.put("33", "浙江");
		areas.put("34", "安徽");
		areas.put("35", "福建");
		areas.put("36", "江西");
		areas.put("37", "山东");
		areas.put("41", "河南");
		areas.put("42", "湖北");
		areas.put("43", "湖南");
		areas.put("44", "广东");
		areas.put("45", "广西");
		areas.put("46", "海南");
		areas.put("50", "重庆");
		areas.put("51", "四川");
		areas.put("52", "贵州");
		areas.put("53", "云南");
		areas.put("54", "西藏");
		areas.put("61", "陕西");
		areas.put("62", "甘肃");
		areas.put("63", "青海");
		areas.put("64", "宁夏");
		areas.put("64", "新疆");
		areas.put("71", "台湾");
		areas.put("81", "香港");
		areas.put("82", "澳门");
		areas.put("91", "国外");
		String[] idcard_array = cardid.split("");
		if (cardid.length() >= 2 && areas.get(cardid.substring(0, 2)) == null) {
			// errors[4]
			return false;
		}
		String regstr = ""; // 正则表达式
		switch (cardid.length()) {
		case 15:
			if ((Integer.parseInt(cardid.substring(6, 2)) + 1900) % 4 == 0
					|| ((Integer.parseInt(cardid.substring(6, 2)) + 1900) % 100 == 0 && (Integer
							.parseInt(cardid.substring(6, 2)) + 1900) % 4 == 0)) {
				regstr = "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"; // 测试出生日期的合法性
			} else {
				regstr = "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"; // 测试出生日期的合法性
			}
			if (Pattern.matches(regstr, cardid)) {
				// errors[0]
				return true;
			} else {
				// errors[2]
				return false;
			}
		case 18:
			if (Integer.parseInt(cardid.substring(6, 4)) % 4 == 0
					|| (Integer.parseInt(cardid.substring(6, 4)) % 100 == 0 && Integer
							.parseInt(cardid.substring(6, 4)) % 4 == 0)) {
				regstr = "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"; // 闰年出生日期的合法性正则表达式
			} else {
				regstr = "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"; // 平年出生日期的合法性正则表达式
			}
			if (Pattern.matches(regstr, cardid)) {
				int s, y;
				s = (Integer.parseInt(idcard_array[0]) + Integer
						.parseInt(idcard_array[10]))
						* 7
						+ (Integer.parseInt(idcard_array[1]) + Integer
								.parseInt(idcard_array[11]))
						* 9
						+ (Integer.parseInt(idcard_array[2]) + Integer
								.parseInt(idcard_array[12]))
						* 10
						+ (Integer.parseInt(idcard_array[3]) + Integer
								.parseInt(idcard_array[13]))
						* 5
						+ (Integer.parseInt(idcard_array[4]) + Integer
								.parseInt(idcard_array[14]))
						* 8
						+ (Integer.parseInt(idcard_array[5]) + Integer
								.parseInt(idcard_array[15]))
						* 4
						+ (Integer.parseInt(idcard_array[6]) + Integer
								.parseInt(idcard_array[16]))
						* 2
						+ Integer.parseInt(idcard_array[7])
						* 1
						+ Integer.parseInt(idcard_array[8])
						* 6
						+ Integer.parseInt(idcard_array[9]) * 3;
				y = s % 11;
				String m = "F", JYM = "10X98765432";
				m = JYM.substring(y, 1);
				if (m == idcard_array[17]) {
					// errors[0]
					return true;
				} else {
					// errors[3]
					return false;
				}
			} else {
				// errors[2]
				return false;
			}
		default:
			// errors[1]
			return false;
		}
	}

	/**
	 * 检测是否为正确的Email
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		if (email == null) {
			return false;
		}
		if (Pattern
				.matches(
						"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",
						email)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确的邮政编码
	 * 
	 * @param postcode
	 * @return
	 */
	public static boolean checkPostcode(String postcode) {
		if (postcode == null) {
			return false;
		}
		if (Pattern.matches("\\d{6}", postcode)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确的电话
	 * 
	 * @param tel
	 * @return
	 */
	public static boolean checkTel(String tel) {
		if (tel == null) {
			return false;
		}
		if (Pattern
				.matches(
						"^((\\+?[0-9]{2,4}\\-[0-9]{3,4}\\-)|([0-9]{3,4}\\-))?([0-9]{7,8})(\\-[0-9]+)?$",
						tel)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否为正确的手机号码
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean checkMobile(String mobile) {
		if (mobile == null) {
			return false;
		}
		return mobile.length() == 11 && checkDigital(mobile);
	}

	/**
	 * 检测是否为正确的地址
	 * 
	 * @param url
	 * @return
	 */
	public static boolean checkURL(String url) {
		if (url == null || "".equals(url)) {
			return false;
		}
		if (Pattern
				.matches(
						"^https?:\\/\\/([\\u4E00-\\u9FA5a-zA-Z\\/\\d\\-]{1,30}\\.){1,3}([\\u4E00-\\u9FA5a-zA-Z]{2,9})(:\\d{1,5})?([\\/|\\\\]{1}[^\\|<>\\?\\*:/\\\\'']+)*([\\/|\\\\]{1}([^\\|<>\\?\\*:/\\\\''\\.]{1,255}\\.[^\\|<>\\?\\*:/\\\\''\\.]{1,255})?)?(\\?)?(&?[\\uFF00-\\uFFFF\\u4E00-\\u9FA5\\w]?=?[\\uFF00-\\uFFFF\\u4E00-\\u9FA5\\w]?)*(#[\\uFF00-\\uFFFF\\u4E00-\\u9FA5\\w])?$",
						url)) {
			return true;
		}
		return false;
	}
}
